package com.qf.springbootopenapi.servlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.captcha.generator.MathGenerator;
import com.qf.springbootopenapi.utils.Constants;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/hutoolCapchar")
public class HutoolCapcharServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //普通验证码
        /*//验证码对象
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 2, 4);
        
        //把验证码的值放入session
        String code = captcha.getCode();
        req.getSession().setAttribute(Constants.HUTOOL_CAPCHAR, captcha);
    
        captcha.write(resp.getOutputStream());*/
        
        //四则运算验证码
        ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 45, 4, 4);
        // 自定义验证码内容为四则运算方式
        captcha.setGenerator(new MathGenerator());
        req.getSession().setAttribute(Constants.HUTOOL_CAPCHAR, captcha);
        captcha.write(resp.getOutputStream());
    }
}
